package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.BaseResponseModel;
import com.chain.BrotherExchange.data.model.CoinOrderList;
import com.chain.BrotherExchange.data.model.LegalTenderOrderDetails;
import com.chain.BrotherExchange.data.model.LegalTenderOrderList;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.utils.EncryptionUtils;

import java.util.List;
import java.util.UUID;

public class OrderViewModel extends AndroidViewModel {

    private int mCoinOrderPage = 1;

    private final int uid = Androids.LocalConfig.getUid();
    private final String token = Androids.LocalConfig.getToken();

    private MutableLiveData<Resource<BaseResponseModel>> mCancelOrderLiveData;
    private MutableLiveData<Resource<List<CoinOrderList.DataBean.ListBean>>> mCoinOrderLiveData;
    private MutableLiveData<Resource<List<CoinOrderList.DataBean.ListBean>>> mMoreCoinOrderLiveData;

    private MutableLiveData<Resource<List<LegalTenderOrderList.DataBean.ListBean>>> mLegalTenderOrderLiveData;
    private MutableLiveData<Resource<List<LegalTenderOrderList.DataBean.ListBean>>> mMoreLegalTenderOrderLiveData;

    private MutableLiveData<Resource<LegalTenderOrderDetails.DataBean>> mLegalTenderOrderDetails;

    private MutableLiveData<Resource<BaseResponseModel>> mConfirmPayment;

    private MutableLiveData<Resource<BaseResponseModel>> mOrderReleaseLiveData;

    private MutableLiveData<Resource<BaseResponseModel>> mCancelAppealLiveData;

    public OrderViewModel(@NonNull Application application) {
        super(application);
        mCancelOrderLiveData = new MutableLiveData<>();
        mCoinOrderLiveData = new MutableLiveData<>();
        mMoreCoinOrderLiveData = new MutableLiveData<>();

        mLegalTenderOrderLiveData = new MutableLiveData<>();
        mMoreLegalTenderOrderLiveData = new MutableLiveData<>();

        mLegalTenderOrderDetails = new MutableLiveData<>();

        mConfirmPayment = new MutableLiveData<>();

        mOrderReleaseLiveData = new MutableLiveData<>();

        mCancelAppealLiveData = new MutableLiveData<>();
    }

    /**
     * 取消币币订单
     */
    public MutableLiveData<Resource<BaseResponseModel>> cancelCoinOrder(String id) {
        String nonce = UUID.randomUUID().toString();
        String sing = EncryptionUtils.generateCancelOrderSign(uid, token, id, nonce);
        DataRepository.get().cancelCoinOrder(uid, token, id, sing, nonce, result -> {
            if (result.isSucceed()) {
                mCancelOrderLiveData.setValue(Resource.success(result, result.getMsg()));
            } else {
                mCancelOrderLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mCancelOrderLiveData;
    }

    /**
     * 获取更多币币订单
     */
    public MutableLiveData<Resource<List<CoinOrderList.DataBean.ListBean>>> moreCoinOrderList() {
        return coinOrderList(++mCoinOrderPage, mMoreCoinOrderLiveData);
    }

    /**
     * 获取新的币币订单
     */
    public MutableLiveData<Resource<List<CoinOrderList.DataBean.ListBean>>> newCoinOrderList() {
        mCoinOrderPage = 1;
        return coinOrderList(mCoinOrderPage, mCoinOrderLiveData);
    }

    /**
     * 币币订单
     */
    private MutableLiveData<Resource<List<CoinOrderList.DataBean.ListBean>>>
    coinOrderList(int page, MutableLiveData<Resource<List<CoinOrderList.DataBean.ListBean>>> liveData) {
        liveData.setValue(Resource.loading());
        DataRepository.get().coinOrderList(uid, token, page, result -> loadState(liveData, result));
        return liveData;
    }

    /**
     * 获取更多未完成币币订单
     */
    public MutableLiveData<Resource<List<CoinOrderList.DataBean.ListBean>>> moreUnfinishedCoinOrder() {
        return unfinishedCoinOrder(++mCoinOrderPage, mMoreCoinOrderLiveData);
    }

    /**
     * 获取新的未完成币币订单
     */
    public MutableLiveData<Resource<List<CoinOrderList.DataBean.ListBean>>> newUnfinishedCoinOrder() {
        mCoinOrderPage = 1;
        return unfinishedCoinOrder(mCoinOrderPage, mCoinOrderLiveData);
    }

    /**
     * 未完成币币订单
     */
    private MutableLiveData<Resource<List<CoinOrderList.DataBean.ListBean>>>
    unfinishedCoinOrder(int page, MutableLiveData<Resource<List<CoinOrderList.DataBean.ListBean>>> liveData) {
        if (Androids.LocalConfig.isLogin()) {
            liveData.setValue(Resource.loading());
            int uid = Androids.LocalConfig.getUid();
            String token = Androids.LocalConfig.getToken();
            DataRepository.get().unfinishedCoinOrder(uid, token, page, result -> loadState(liveData, result));
        }
        return liveData;
    }

    /**
     * 获取新的法币订单
     */
    public MutableLiveData<Resource<List<LegalTenderOrderList.DataBean.ListBean>>> newLegalTenderOrder() {
        mCoinOrderPage = 1;
        legalTenderOrder(mCoinOrderPage, mLegalTenderOrderLiveData);
        return mLegalTenderOrderLiveData;
    }

    /**
     * 获取更多法币订单
     */
    public MutableLiveData<Resource<List<LegalTenderOrderList.DataBean.ListBean>>> moreLegalTenderOrder() {
        legalTenderOrder(++mCoinOrderPage, mMoreLegalTenderOrderLiveData);
        return mMoreLegalTenderOrderLiveData;
    }

    /**
     * 法币订单详情
     */
    public MutableLiveData<Resource<LegalTenderOrderDetails.DataBean>> legalTenderOrderDetails(String id) {
        mLegalTenderOrderDetails.setValue(Resource.loading());
        DataRepository.get().legalTenderOrderDetails(uid, token, id, result -> {
            if (result.isSucceed()) {
                mLegalTenderOrderDetails.setValue(Resource.success(result.getData()));
            } else {
                mLegalTenderOrderDetails.setValue(Resource.error(result.getMsg()));
            }
        });

        return mLegalTenderOrderDetails;
    }

    /**
     * 确定支付
     */
    public MutableLiveData<Resource<BaseResponseModel>> confirmPayment(int id) {
        mConfirmPayment.setValue(Resource.loading());
        DataRepository.get().confirmPayment(uid, token, id, result -> {
            if (result.isSucceed()) {
                mConfirmPayment.setValue(Resource.success(result.getMsg()));
            } else {
                mConfirmPayment.setValue(Resource.error(result.getMsg()));
            }
        });

        return mConfirmPayment;
    }

    /**
     * 取消法币订单
     */
    public MutableLiveData<Resource<BaseResponseModel>> cancelLegalTenderOrder(int id, int type) {
        mCancelOrderLiveData.setValue(Resource.loading());
        DataRepository.get().cancelLegalTenderOrder(uid, token, id, type, result -> {
            if (result.isSucceed()) {
                mCancelOrderLiveData.setValue(Resource.success(result.getMsg()));
            } else {
                mCancelOrderLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mCancelOrderLiveData;
    }

    /**
     * 法币订单放行
     */
    public MutableLiveData<Resource<BaseResponseModel>> orderRelease(int id, String password){
        mOrderReleaseLiveData.setValue(Resource.loading());
        String nonce = UUID.randomUUID().toString();
        String sign = EncryptionUtils.orderRelease(uid, token, id, password, nonce);
        DataRepository.get().orderRelease(uid, token, id, password, sign, nonce, result -> {
            if (result.isSucceed()) {
                mOrderReleaseLiveData.setValue(Resource.success(result.getMsg()));
            } else {
                mOrderReleaseLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mOrderReleaseLiveData;
    }

    /**
     * 取消申诉
     */
    public MutableLiveData<Resource<BaseResponseModel>> cancelAppeal(int appealId){
        mCancelAppealLiveData.setValue(Resource.loading());
        DataRepository.get().cancelAppeal(uid, token, appealId, result -> {
            if (result.isSucceed()) {
                mCancelAppealLiveData.setValue(Resource.success(result.getMsg()));
            } else {
                mCancelAppealLiveData.setValue(Resource.error(result.getMsg()));
            }
        });
        return mCancelAppealLiveData;
    }

    /**
     * 法币订单
     */
    private void legalTenderOrder(
            int page, MutableLiveData<Resource<List<LegalTenderOrderList.DataBean.ListBean>>> liveData) {
        liveData.setValue(Resource.loading());
        DataRepository.get().legalTenderOrderList(uid, token, page, result -> {
            LegalTenderOrderList.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                List<LegalTenderOrderList.DataBean.ListBean> list = resultData.getList();
                if (list != null && !list.isEmpty()) {
                    liveData.setValue(Resource.success(list));
                } else {
                    liveData.setValue(Resource.empty());
                }
            } else {
                liveData.setValue(Resource.error(result.getMsg()));
            }
        });
    }

    /**
     * 加载币币订单状态
     */
    private void loadState(
            MutableLiveData<Resource<List<CoinOrderList.DataBean.ListBean>>> liveData,
            CoinOrderList result) {
        CoinOrderList.DataBean resultData = result.getData();
        if (result.isSucceed() && resultData != null) {
            List<CoinOrderList.DataBean.ListBean> list = resultData.getList();
            if (list != null && !list.isEmpty()) {
                liveData.setValue(Resource.success(list));
            } else {
                liveData.setValue(Resource.empty());
            }

        } else if (result.isNotNetwork()) {
            liveData.setValue(Resource.notNetwork());
        } else if (result.isLogonFailure()) {
            liveData.setValue(Resource.tokenError(result.getMsg()));
        } else {
            liveData.setValue(Resource.error(result.getMsg()));
        }
    }
}
